package com.sherry.servlet;

import com.alibaba.fastjson.JSONObject;
import com.sherry.model.Order;
import com.sherry.service.IOrderService;
import com.sherry.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/order/update")
public class UpdateOrderServlet extends HttpServlet {
    // 处理post请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端POST请求");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        System.out.println("接收到前端修改订单请求");
        String updateNo=request.getParameter("orderNo");
        String orderPlace= request.getParameter("orderPlace");
        String companyName=request.getParameter("companyName");
        Integer userId=Integer.parseInt(request.getParameter("userId"));
        Integer orderId=Integer.parseInt(request.getParameter("orderId")) ;
        Double totalAccount=Double.parseDouble(request.getParameter("totalAccount"));
        String createTimeStr = request.getParameter("createTime");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parsedDate = null;
        try {
            parsedDate = sdf.parse(createTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Timestamp createTime = new Timestamp(parsedDate.getTime());
        System.out.println("SL:"+updateNo+" "+orderPlace+" "+companyName+" "+createTime);

        IOrderService orderService=new OrderServiceImpl();
        Integer i= orderService.updateOrder(new Order( orderId,userId,updateNo,totalAccount, (Timestamp) createTime,companyName,orderPlace));
        boolean flag=false;
        if (i>0){
            flag=true;
        }
        JSONObject data=new JSONObject();
        data.put("flag",flag);

        String dataStr=data.toJSONString();
        //响应
        PrintWriter write = response.getWriter();
        write.write(dataStr);
        write.flush();
    }

    // 处理get请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端GET请求");
    }
}